//
//  YSSubProductCategoryView.m
//  Yueshi
//
//  Created by Northwan on 15/8/11.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//

#import "YSSubProductCategoryView.h"
#import "YSProductCategory.h"
#import "CustomShowView.h"

#define BTN_HEIGHT 20
#define BTN_WIDTH 60
#define DEFAULT_MARGIN 25

@interface YSSubProductCategoryView()
@property (strong, nonatomic)NSMutableArray *categoryArray;
@property (strong, nonatomic)NSMutableArray *categoryViewArray;
@property (strong, nonatomic)UIView *containerView;

@end

@implementation YSSubProductCategoryView

-(NSMutableArray *)categoryArray{
    if(!_categoryArray){
        _categoryArray = [NSMutableArray array];
    }
    return _categoryArray;
}

-(NSMutableArray *)categoryViewArray{
    if(!_categoryViewArray){
        _categoryViewArray = [NSMutableArray array];
    }
    return _categoryViewArray;
}

-(instancetype)initWithCategoryArray:(NSArray *)categoryArray{
    self = [super init];
    if(self){
        self.userInteractionEnabled = YES;
        self.containerView = [[UIView alloc]initWithFrame:CGRectMake(DEFAULT_MARGIN, 0,SCREENW-DEFAULT_MARGIN*2, 0)];
        self.userInteractionEnabled = YES;
        self.containerView.userInteractionEnabled = YES;
        [self addSubview:_containerView];
        [self.categoryArray addObjectsFromArray:categoryArray];
        [self setTagArray:categoryArray];
    }
    return self;
}

- (void)setTagArray:(NSArray *)array{
    [self.containerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.categoryViewArray removeAllObjects];
    for (int i = 0; i<array.count; i++) {
        YSProductCategory *category = array[i];
        [self addBtnWithCategory:category];
    }
}

- (void)addBtnWithCategory:(YSProductCategory *)category{
    UIButton *button = [[UIButton alloc] init];
    button.backgroundColor = [UIColor clearColor];
    [button setTag:category.categoryId];
    [button setTitle:category.categoryName forState:UIControlStateNormal];
    [button.titleLabel setFont:SYS_FONT(13)];
    button.userInteractionEnabled  = YES;
    [button setEnabled:YES];
    [button setCornerRadius:5 borderWidth:0.5 borderColor:HEXColor(@"#b9b9b9")];
    [button setTitleColor:HEXColor(@"#898989") forState:UIControlStateNormal];
    [button addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:ThemeColor forState:UIControlStateSelected];
    [self.containerView addSubview:button];
    [self.categoryViewArray addObject:button];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat parentW =  self.width;
    CGFloat currentX = 0;
    CGFloat currentY = 10;
    for (int i = 0; i<self.categoryViewArray.count; i++) {
        UIButton *button = self.categoryViewArray[i];
        [button sizeToFit];
        button.width = BTN_WIDTH;
        button.height = BTN_HEIGHT;
        // margin = 当前View的宽度-两边的默认间距-四个btn的间距/3
        CGFloat margin =  (parentW-DEFAULT_MARGIN*2-BTN_WIDTH*4)/3;
        
        if (button.width+currentX>parentW-DEFAULT_MARGIN*2) {
            button.x = 0;
            button.y = currentY + BTN_HEIGHT * 1.5;
        }else{
            button.x = currentX;
            button.y = currentY;
        }
        currentY = button.y;
        currentX = CGRectGetMaxX(button.frame)+margin;
    }
    self.containerView.height = CGRectGetMaxY([[self.categoryViewArray lastObject] frame])+YSPublicXInset;
    self.height = self.containerView.height;
}

-(void)itemClick:(UIButton *)sender{
    if(sender == nil){
        return;
    }
    if([_delegate respondsToSelector:@selector(categoryItemClick:)]){
        [_delegate categoryItemClick:sender.tag];
    }
    
   CustomShowView *showView = (CustomShowView *) self.superview;
    [showView hideWithAnim:YES];
}

@end
